﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAPL.Language.Core.Types;

namespace YAPL.Language.Core {
	/// <summary>
	/// Defines collection for function scope variables
	/// </summary>
	public class Scope : IEnumerable<KeyValuePair<string, YAPLObject>> {
		private readonly Dictionary<string, YAPLObject> _objects = new Dictionary<string, YAPLObject>();
		
		public YAPLObject this[string key] {
			get {
				if (!_objects.ContainsKey(key))
					return YAPLObject.UNDEFINED;
				return _objects[key];
			}
			set {
				_objects[key] = value;
			}
		}

		public void ReplaceKey(string oldKey, string newKey) {
			if (_objects.ContainsKey(oldKey)) {
				var value = _objects[oldKey];
				_objects.Remove(oldKey);
				_objects[newKey] = value;
			}
		}

		public IEnumerator<KeyValuePair<string, YAPLObject>> GetEnumerator() {
			foreach (var obj in _objects) {
				yield return obj;
			}
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}
	}
}
